package com.yanqu.road.dao.impl.activity.zhenhuan.thirdProcess;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.thirdProcess.CrossZhenHuanThirdProcessUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossZhenHuanThirdProcessUserDataDaoImpl extends TempDataObjectDao<CrossZhenHuanThirdProcessUserData> {

    public static String getDataSql = "select * from `t_c_zhenhuan_third_process_user_data` where `activityId` = ?";
    public Map<Long, CrossZhenHuanThirdProcessUserData> getCrossZhenHuanThirdProcessUserDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossZhenHuanThirdProcessUserData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossZhenHuanThirdProcessUserData data = parseData(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public static String updateCrossZhenHuanThirdProcessUserDataSql = "update `t_c_zhenhuan_third_process_user_data` set level=?,prestige=?,lastUpdateTime=?,pos=?,score=?,banquetNum=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(CrossZhenHuanThirdProcessUserData crossZhenHuanThirdProcessUserData) {
        crossZhenHuanThirdProcessUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanThirdProcessUserData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanThirdProcessUserData.getPrestige()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanThirdProcessUserData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanThirdProcessUserData.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanThirdProcessUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanThirdProcessUserData.getBanquetNum()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanThirdProcessUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanThirdProcessUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossZhenHuanThirdProcessUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossZhenHuanThirdProcessUserDataSql = "insert into `t_c_zhenhuan_third_process_user_data`(activityId,userId,level,prestige,lastUpdateTime,pos,score,banquetNum) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossZhenHuanThirdProcessUserData crossZhenHuanThirdProcessUserData) {
        crossZhenHuanThirdProcessUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanThirdProcessUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanThirdProcessUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanThirdProcessUserData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanThirdProcessUserData.getPrestige()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanThirdProcessUserData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanThirdProcessUserData.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanThirdProcessUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanThirdProcessUserData.getBanquetNum()));
        if (getDb().executeNoneQuery(insertCrossZhenHuanThirdProcessUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossZhenHuanThirdProcessUserData parseData(ResultSet rs) throws SQLException {
        CrossZhenHuanThirdProcessUserData crossZhenHuanThirdProcessUserData = new CrossZhenHuanThirdProcessUserData();
        crossZhenHuanThirdProcessUserData.setActivityId(rs.getInt("activityId"));
        crossZhenHuanThirdProcessUserData.setUserId(rs.getLong("userId"));
        crossZhenHuanThirdProcessUserData.setLevel(rs.getInt("level"));
        crossZhenHuanThirdProcessUserData.setPrestige(rs.getLong("prestige"));
        crossZhenHuanThirdProcessUserData.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        crossZhenHuanThirdProcessUserData.setPos(rs.getInt("pos"));
        crossZhenHuanThirdProcessUserData.setScore(rs.getLong("score"));
        crossZhenHuanThirdProcessUserData.setBanquetNum(rs.getInt("banquetNum"));
        return crossZhenHuanThirdProcessUserData;
    }

}
